package com.example.Repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.Repository.CommentRepository;
import com.example.Repository.TrendsRepository;
import com.example.dao.CommentDao;
import com.example.pojo.*;
import com.example.pojo.dto.TrendsDto;
import com.example.pojo.vo.CommentVo;
import com.example.pojo.vo.TrendsVo;
import com.example.pojo.vo.UserVo;
import com.example.service.*;
import com.example.utils.ConditionVo;
import com.example.utils.SearchUtil;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author crt
 */
@Service
public class TrendsRepositoryImpl implements TrendsRepository {

    @Resource
    private TrendsService trendsService;
    @Resource
    private UserService userService;
    @Resource
    private FileService fileService;
    @Resource
    private TagService tagService;
    @Resource
    private UserTrendsService userTrendsService;
    @Resource
    private TrendsFileService trendsFileService;
    @Resource
    private TrendsTagService trendsTagService;
    @Resource
    private CommentDao commentDao;
    @Resource
    private CommentRepository commentRepository;
    @Resource
    private TrendsLikeService trendsLikeService;
    @Resource
    private TrendsCollectService trendsCollectService;

    @Override
    public Result<String> addTrends(TrendsDto trendsDto) {

        Trends trends = new Trends();
        trends.setTrendsTitle(trendsDto.getTrendsTitle());
        trends.setTrendsContent(trendsDto.getTrendsContent());
        trends.setTime(new Date());
        trendsService.save(trends);

        UserTrends userTrends = new UserTrends();
        userTrends.setUserId(trendsDto.getUserId());
        userTrends.setTrendsId(trends.getId());
        userTrendsService.save(userTrends);

        //判断帖子是否有tag
        if (!CollectionUtils.isEmpty(trendsDto.getTagIdList())){
            List<TrendsTag> trendsTags = new ArrayList<>();
            for ( Integer tagId : trendsDto.getTagIdList()){
                TrendsTag trendsTag = new TrendsTag();
                trendsTag.setTrendsId(trends.getId());
                trendsTag.setTagId(tagId);
                trendsTags.add(trendsTag);
            }
            trendsTagService.saveBatch(trendsTags);
        }

        //判定保存时文件是否上传
        if(CollectionUtils.isEmpty(trendsDto.getFileIdList())){
            throw new RuntimeException("文件列表为空");
        }

        List<TrendsFile> trendsFiles = new ArrayList<>();
        for ( Integer fileId : trendsDto.getFileIdList()){
            TrendsFile trendsFile = new TrendsFile();
            trendsFile.setTrendsId(trends.getId());
            trendsFile.setFileId(fileId);
            trendsFiles.add(trendsFile);
        }
        trendsFileService.saveBatch(trendsFiles);


        return Result.success("添加帖子成功");
    }

    @Override
    public Result<TrendsVo> getTrendsInfo(Integer trendsId) {
        TrendsVo trendsVo = getTrendsVo(trendsId);
        return Result.success(trendsVo);
    }

    @Override
    public Result<List<TrendsVo>> getTrendsList(List<ConditionVo> conditionVoList) {

        List<TrendsVo> trendsVoList = getTrendsVoList(conditionVoList);

        return Result.success(trendsVoList);
    }

    @Override
    public Result<String> deleteTrends(Integer trendsId) {
        QueryWrapper queryWrapper = new QueryWrapper<>().eq("trends_id",trendsId);
        trendsTagService.remove(queryWrapper);
        //删除文件需重写
        trendsFileService.remove(queryWrapper);
        //获取第一层评论
        List<CommentVo> commentVoList = commentDao.getFirstCommentList(trendsId);
        for (CommentVo commentVo : commentVoList){
            //删除评论
            commentRepository.deleteComment(commentVo.getCommentId());
        }
        trendsLikeService.remove(queryWrapper);
        trendsCollectService.remove(queryWrapper);
        userTrendsService.remove(queryWrapper);
        QueryWrapper<Trends> queryWrapper1 = new QueryWrapper<Trends>().eq("id",trendsId);
        trendsService.remove(queryWrapper1);

        return Result.success("删除成功");
    }


    public List<TrendsVo> getTrendsVoList(List<ConditionVo> conditionVoList) {
        QueryWrapper<UserTrends> queryWrapper = SearchUtil.parseWhereSql(conditionVoList);
        List<UserTrends> userTrendsList =  userTrendsService.list(queryWrapper);
        List<TrendsVo> trendsVoList = new ArrayList<>();
        for(UserTrends userTrends : userTrendsList){
            TrendsVo trendsVo = getTrendsVo(userTrends.getTrendsId());
            trendsVoList.add(trendsVo);
        }
        return trendsVoList;
    }

    public TrendsVo getTrendsVo(Integer trendsId){
        Trends trends = trendsService.getOne(new QueryWrapper<Trends>().eq("id", trendsId));
        UserTrends userTrends = userTrendsService.getOne(new QueryWrapper<UserTrends>().eq("trends_id", trendsId));
        User user = userService.getOne(new QueryWrapper<User>().eq("id",userTrends.getUserId()));
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(user,userVo);
        List<TrendsFile> trendsFiles = trendsFileService.list(new QueryWrapper<TrendsFile>().eq("trends_id",trendsId));
        List<Integer> fileIds =  trendsFiles.stream().map(TrendsFile::getFileId).collect(Collectors.toList());
        List<String> fileUrlList = new ArrayList<>();
        if(!fileIds.isEmpty()){
            fileUrlList = fileService.listByIds(fileIds).stream().map(File::getFileUrl).toList();
        }
        List<TrendsTag> trendsTags = trendsTagService.list(new QueryWrapper<TrendsTag>().eq("trends_id",trendsId));
        List<Integer> tagIds =  trendsTags.stream().map(TrendsTag::getTagId).collect(Collectors.toList());
        List<Tag> tagList = new ArrayList<>();
        if(!tagIds.isEmpty()){
            tagList = tagService.listByIds(tagIds);
        }
        TrendsVo trendsVo = new TrendsVo();
        trendsVo.setTrendsId(trends.getId());
        trendsVo.setTrendsTitle(trends.getTrendsTitle());
        trendsVo.setTrendsContent(trends.getTrendsContent());
        trendsVo.setUserVo(userVo);
        trendsVo.setFileList(fileUrlList);
        trendsVo.setTagList(tagList);
        return trendsVo;
    }
}
